package com.ford.trade.service.impl;

import com.ford.trade.entity.Game;
import com.ford.trade.repository.GameRepository;
import com.ford.trade.service.Game500Service;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

/**
 * @author :  zhangfutao
 * @version :  V-1.0
 * @description :
 * @date :   2022/5/24 14:34
 */
@Service
public class Game500ServiceImpl implements Game500Service {

    @Autowired
    private GameRepository gameRepository;

    @Override
    public void save(Game game) {

        Game byIndex = gameRepository.findByIndexId(game.getIndexId());
        if (null != byIndex) {
            game.setId(byIndex.getId());
        }
        gameRepository.save(game);
    }

    @Override
    public List<Game> findAllByDateAfter(String date) {
        return gameRepository.findAllByDateAfter(date);
    }

    @Override
    public List<Game> findAllByDateAfterAndType(String date, String type) {
        return gameRepository.findAllByDateAfterAndType(date, type);
    }

    @Override
    public List<Game> findAllByDateBetten(String date) throws ParseException {
        Date addDays = DateUtils.addDays(DateUtils.parseDate(date, "yyyy-MM-dd HH:mm:ss"), 1);
        String format = DateFormatUtils.format(addDays, "yyyy-MM-dd HH:mm:ss");
        return gameRepository.findByDateBetween(date, format);
    }
}
